/*
 * Copyright (c) 2009, Manish Shakya,Real Time Solutions Pvt. Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 */
/**
*
*	\file  uart1.c
*	\author Manish Shakya 
*	\brief
*	\date  Tuesday,October 27,2009
*	\version 1.0
*
*
*/
#include "peripherals.h"

#ifdef __UART1__
	#if WARNING_ENABLE == 1
		#warning "Please Check PLL Settings and Baudrate Reload Value" 
	#endif

	#if UART1_DEBUG == 1
	//	#define UART1_BAUDRATE 8
	//#define UART1_CONTROL	(CHAR_LEN_8_BITS|STOP_BIT_1|PARITY_DISABLE)

		#define UART1_INTERRUPT 		0
		#define UART1_INTERRUPT_TX 		0
		#define UART1_INTERRUPT_RX 		0
		#define UART1_INTERRUPT_ERROR 	0
	#endif

	#ifndef UART1_BAUDRATE
		#warning "Please define UART1 Baudrate"
	#endif
	
	#ifndef UART1_CONTROL
		#warning "Please define UART1 databits,stop bits,parity..etc"
	#endif


#if UART1_INTERRUPT == 1 
/***********************************************************************************************************************/
 __irq void 
 __uart1_isr(void)
{
   		unsigned char c;
   		c=U1IIR;
		
		#if UART1_INTERRUPT_TX==1
		if(c&0x02){
			uart1_tx_isr();
		}
		#endif
	
		/*error in serial comm*/
		#if UART1_INTERRUPT_ERROR ==1
		if(U1LSR &0x80){
			uart1_error_isr();
			c=U1RBR;
		}
		#endif
		
		/*char to received*/
		#if UART1_INTERRUPT_RX == 1
		if(U1LSR&0x01){
			c=U1RBR;
			uart1_rx_isr(c);
		}
		#endif

		VICVectAddr=0x000000000;
}
#endif

/***********************************************************************************************************************/
void uart1_init(void)
{
		
		#ifndef  UART1_CONTROL
			#error "Define UART1_CONTROL in app-config.h"
		#endif

		#ifndef UART1_BAUDRATE
			#error "Define UART1_BAUDRATE in app-config.h"
		#endif
	 
	  PINSEL0 	|= 0x00050000;         /* Enable RxD1 and TxD1              */ 
	  U1LCR 	= UART1_CONTROL|0x80;         /* 8 bits, no Parity, 1 Stop bit     */
	  U1DLL 	= (unsigned char)(UART1_BAUDRATE&0x00FF); 
  	  U1DLM 	= (unsigned char)((UART1_BAUDRATE&0xFF00)>>8); 
	  U1LCR 	= UART1_CONTROL;        /* DLAB = 0  */ 
	
		#if UART1_DEBUG == 0
		#if UART1_INTERRUPT== 1
			#if	UART1_INTERRUPT_RX== 1
			U1IER		|=	0x1;			/* Enable receive int */
			#endif
			#if	UART1_INTERRUPT_TX== 1
			U1IER		|=	0x2;			/* Enable receive int */
			#endif
			#if	UART1_INTERRUPT_ERROR== 1
			U0IER		|=	0x4;			/* Enable receive int */
			#endif

			irq_install(UART1_INT,__uart1_isr);
		#endif
		#endif
}


#if UART1_INTERRUPT_TX == 0
/***********************************************************************************************************************/
char uart1_put_char(char c)
{
		U1THR=c;
		while(!(U1LSR&0x20)); 
		return c;
}

/***********************************************************************************************************************/
void uart1_put_str(char * str)
{
	while(*str)
	{
		uart1_put_char(*str);
		++str;
	}
}

/***********************************************************************************************************************/ 

void uart1_put(void * str,uint32_t len)
{
	uint8_t *ptr=str;
	for(;len>0;--len)
	{
		uart1_put_char(*ptr);
		++ptr;
	}
}
#endif


#if UART1_INTERRUPT_RX == 0
/***********************************************************************************************************************/

unsigned char uart1_char(void)
{
	return ((U1LSR&0x01));
}

/***********************************************************************************************************************/
																														 /***********************************************************************************************************************/
uint8_t uart1_get_char(void)
{
		uint8_t c;
		while(!(U1LSR&0x01));
		c=U1RBR;
		return c;
}
#endif

#if UART1_DEBUG == 1

#include <stdio.h>
#include <rt_misc.h>

#pragma import(__use_no_semihosting_swi)

struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;


int fputc(int ch, FILE *f) {
  return (uart1_put_char(ch));
}


int ferror(FILE *f) {
  /* Your implementation of ferror */
  return EOF;
}


void _ttywrch(int ch) {
  uart1_put_char(ch);
}


void _sys_exit(int return_code) {
label:  goto label;  /* endless loop */
}


#endif
#endif

